package com.example.g32.home

import android.os.Bundle
import android.util.Log
import android.util.TimeUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.paging.filter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.NoticeEntityItem
import com.example.base.entity.PagingListData
import com.example.base.mvi.BaseFragment
import com.example.base.mvi.RequestType
import com.example.base.mvi.UiState

import com.example.g32.R
import com.example.g32.databinding.FragmentHomeBinding
import com.example.g32.paging.NoticePageAdapter
import com.example.g32.paging.OnItemClickListener
import com.example.g32.paging.OnItemLongClickListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding, HomeIntent, HomeVIewModel>() {
    override val binding by lazy {
        FragmentHomeBinding.inflate(layoutInflater)
    }
    override val vm by viewModels<HomeVIewModel>()

    val homeAdapter by lazy { HomeAdapter() }

    val noticePageAdapter by lazy { NoticePageAdapter() }

    override fun initView() {
        homeAdapter.setOnItemClickListener { adapter, view, position ->

        }

        binding.homeHeader.apply {
            headBack.setImageResource(R.drawable.ic_action_account)
            headTitle.visibility = View.GONE
            headNotice.visibility = View.VISIBLE
            headAdd.setImageResource(R.drawable.ic_action_account)
        }

        vm.sendIntent(HomeIntent.getNotice(1,20))
        binding.rvNotice.adapter = noticePageAdapter

        noticePageAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(position: Int, bean: NoticeEntityItem) {
                Log.d("TAG", "${position}-----${bean} ")
            }
        })

        noticePageAdapter.setOnItemLongClickListener(object : OnItemLongClickListener{
            override fun onItemLongClick(position: Int, bean: NoticeEntityItem): Boolean {
                ToastUtils.showLong("${position}")
                return true
            }

        })
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*>->{
//                        val item = it.data as Collection<NoticeEntityItem>
//                        homeAdapter.addData(item)
//                        homeAdapter.notifyDataSetChanged()
                        when(it.type){
                            RequestType.DELETE ->{

                            }
                           else->{
                                val item = it.data as PagingData<NoticeEntityItem>
                                noticePageAdapter.submitData(item.filter {
                                    it.title.isNotEmpty()
                                })
                            }
                        }


                    }

                    is UiState.onError->{
                        Log.d("xs", it.msg.toString())
                    }
                    else ->{}
                }
            }
        }
    }

}